package com.bigdata.config.mysqlconfigure;/**
 * Created by Administrator on 2019/7/26 0026.
 */

import com.alibaba.druid.pool.DruidDataSource;
import com.bigdata.config.mysqlconfigure.properties.DruidStatProperties;
import com.bigdata.config.mysqlconfigure.properties.MysqlMybatisProperties;
import com.bigdata.config.mysqlconfigure.stat.DruidFilterConfiguration;
import com.bigdata.config.mysqlconfigure.stat.DruidSpringAopConfiguration;
import com.bigdata.config.mysqlconfigure.stat.DruidStatViewServletConfiguration;
import com.bigdata.config.mysqlconfigure.stat.DruidWebStatFilterConfiguration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


/**
 * @author Administrator
 * @description
 * @date 2019/7/26 0026
 **/
@Configuration
@EnableConfigurationProperties({DruidStatProperties.class, DataSourceProperties.class, MysqlMybatisProperties.class})
@MapperScan(basePackages = MysqlDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
@ConditionalOnClass(DruidDataSource.class)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@Import({DruidSpringAopConfiguration.class,
        DruidStatViewServletConfiguration.class,
        DruidWebStatFilterConfiguration.class,
        DruidFilterConfiguration.class})
public class MysqlDataSourceConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(MysqlDataSourceConfig.class);

    static final String PACKAGE = "com.bigdata.mysql.dao";

    @Autowired
    MysqlMybatisProperties oracleMybatisProperties;

    @Bean(initMethod = "init", name = "mysqlDataSource")
    @Primary
    public DataSource dataSource() {
        LOGGER.info("Init DruidDataSource");
        return new DruidDataSourceWrapper();
    }

    @Bean(name = "mysqlTransactionManager")
    @Primary
    public DataSourceTransactionManager clusterTransactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean(name = "mysqlSqlSessionFactory")
    @Primary
    public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource clusterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(clusterDataSource);
        sessionFactory.setMapperLocations(oracleMybatisProperties.resolveMapperLocations());
        return sessionFactory.getObject();
    }
}
